package com.fxd.shopping.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fxd.common.vo.SkuHasStockVo;
import com.fxd.core.util.KeyUtils;
import com.fxd.core.util.PageUtils;
import com.fxd.core.util.Query;
import com.fxd.core.util.RR;
import com.fxd.feigns.ProductFeign;
import com.fxd.model.ware.WareSkuEntity;
import com.fxd.shopping.ware.dao.WareSkuDao;
import com.fxd.shopping.ware.service.WareSkuService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    ProductFeign productFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String skuId = KeyUtils.get(params, "skuId");
        if (StringUtils.isNotBlank(skuId)) {
            wrapper.eq("sku_id", skuId);
        }
        String wareId = KeyUtils.get(params, "wareId");
        if (StringUtils.isNotBlank(wareId)) {
            wrapper.eq("ware_id", wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //如果还没有这个商品的库存记录
        List<WareSkuEntity> wareSkuEntities = this.baseMapper.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (CollectionUtils.isEmpty(wareSkuEntities)) {
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStockLocked(0);
            //查询sku名称
            RR info = productFeign.info(skuId);
            if (info.getCode() == 0) {
                Map<String, Object> skuInfo = (Map<String, Object>) info.get("skuInfo");
                wareSkuEntity.setSkuName((String) skuInfo.get("skuName"));
            }
            this.baseMapper.insert(wareSkuEntity);
        } else {
            this.baseMapper.addStock(skuId, wareId, skuNum);
        }

    }

    @Override
    public List<SkuHasStockVo> getSkusHasStock(List<Long> skuIds) {
        // 直接分组一次性查询
        List<WareSkuEntity> wareSkuEntities = lambdaQuery().select(WareSkuEntity::getSkuId, WareSkuEntity::getStockSum).in(WareSkuEntity::getSkuId, skuIds)
                .groupBy(WareSkuEntity::getSkuId).list();
        List<SkuHasStockVo> collect = wareSkuEntities.stream().map(item -> {
            SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
            skuHasStockVo.setSkuId(item.getSkuId());
            skuHasStockVo.setHasStock(item.getStockSum() > 0);
            return skuHasStockVo;
        }).collect(Collectors.toList());
        return collect;
    }

}